const pt = require('puppeteer');

//爬取'推荐课程'
const base_url = 'https://wenku.baidu.com/p/',
    url = [`${base_url}守候爱的铁树`,
           `${base_url}shoudushifan9`,
           `${base_url}北京周垂坤律师`,
           `${base_url}旗胜管理咨询`,
           `${base_url}njbaige`,
           `${base_url}yangyanxiu88`,
           `${base_url}沙基瑞林`,
           `${base_url}专业修复洗发水`,
    ];

const data = [];

async function Crawler() {
    try {
        const bs = await pt.launch(),
            pg = await bs.newPage();

        for (var i = 0; i < url.length; i++) {
            await pg.goto(url[i], {
                timeout: 0,
                waitUntil: 'networkidle2'
            });
           
            const result = await pg.evaluate(function () {
                const $ = window.$,
                      $item = $('.brief-wrap .con');

                const dataItem = {
                    aid: $item.find('.avatar img').prop('src').match(/\/item\/(.*?).jpg/)[1],
                    name: $item.find('.author').text(),
                    authorPosition: $item.find('.title').text(),//作者的职位
                    imgUrl: $item.find('.avatar img').prop('src'),
                    desStatistics: $item.find('.des .statistics').text(),
                    desDetail: $item.find('.des .detail').text(),
                    imgKey: ''
                }

                return dataItem;
            });

            data.push(result);
        }

        await bs.close();

        process.send(data);

        setTimeout(() => {
            process.exit(0);
        }, 1000);
    } catch (error) {
        console.log(error);
    }
}

Crawler();



